<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="学校名称" prop="schoolName">
            <el-input
              v-model="queryParams.schoolName"
              placeholder="请输入学校名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称" prop="demandName">
            <el-input
              v-model="queryParams.demandName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否确认">
            <el-select
              v-model="queryParams.confirm"
              placeholder="请选择项目状态"
              size="small"
            >
              <el-option
                v-for="item in confirmStateOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目进度">
            <el-select
              v-model="queryParams.jd"
              placeholder="请选择项目状态"
              size="small"
            >
              <el-option
                v-for="item in jdOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="9">
          <el-form-item label="申报时间">
            <el-date-picker
              clearable
              size="small"
              v-model="queryParams.selectStartTime"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              range-separator="至"
              type="daterange"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archives:purchase:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:demand:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:demand:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['archives:purchase:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="demandList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="学校名称"
        align="center"
        prop="schoolName"
        width="200"
      />
      <el-table-column
        label="项目名称"
        align="center"
        prop="demandName"
        width="200"
      />
      <el-table-column
        label="投资总额/万元"
        align="center"
        prop="totalinvestment"
        width="120"
      />
      <el-table-column
        label="当年预算金额/万元"
        align="center"
        prop="budget"
        width="120"
      />
      <el-table-column label="当前进度" align="center" prop="jdId" width="110">
        <template slot-scope="scope">
          <!-- <el-tag v-if="parseInt(scope.row.jdId) === 1">项目备案</el-tag> -->
          <el-tag v-if="parseInt(scope.row.jdId) <= 5">项目备案</el-tag>
          <el-tag v-if="parseInt(scope.row.jdId) === 6" type="info"
            >项目公示</el-tag
          >
          <el-tag v-if="parseInt(scope.row.jdId) === 7" type="warning"
            >项目中标</el-tag
          >
          <el-tag v-if="parseInt(scope.row.jdId) === 8" type="danger"
            >项目施工</el-tag
          >
          <el-tag v-if="parseInt(scope.row.jdId) >= 9" type="success"
            >项目验收</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="备案时间"
        align="center"
        prop="beianTime"
        width="110"
      />
      <el-table-column
        label="公示时间"
        align="center"
        prop="gongshiTime"
        width="110"
      />
      <el-table-column
        label="开标时间"
        align="center"
        prop="kaibiaoTime"
        width="110"
      />
      <el-table-column
        label="中标时间"
        align="center"
        prop="zhongbiaoTime"
        width="110"
      />
      <el-table-column
        label="中标金额/万元"
        align="center"
        prop="bidMoney"
        width="120"
      />
      <el-table-column
        label="中标供应商"
        align="center"
        prop="bidSupplier"
        width="100"
      />
      <el-table-column
        label="合同时间"
        align="center"
        prop="contractTime"
        width="110"
      />
      <el-table-column
        label="验收时间"
        align="center"
        prop="yanshouTime"
        width="110"
      />
      <el-table-column
        label="第一次支付金额//万元"
        align="center"
        prop="firstMoney"
        width="150"
      />
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="110"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="110"
      />
      <el-table-column
        label="是否确认"
        align="center"
        prop="confirm"
        width="110"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.confirm === 0" type="info">未确认</el-tag>
          <el-tag v-if="scope.row.confirm === 1">已确认</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="320"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="confirmBtnClick(scope.row)"
            v-hasPermi="['archives:purchase:confirm']"
            >确认</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder-remove"
            @click="recallBtnClick(scope.row)"
            v-hasPermi="['archives:purchase:recall']"
            >撤回</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row, 0)"
            v-hasPermi="['archives:purchase:look']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['archives:purchase:edit']"
            :disabled="scope.row.confirm === 1"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archives:purchase:remove']"
            :disabled="scope.row.confirm === 1"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
            v-hasPermi="['archives:purchase:download']"
            >下载</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购类项目需求申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
      v-dialogDrag
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="demandName">
          <el-input v-model="form.demandName" placeholder="请输入项目名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="editFormTitle"
      :visible.sync="editDialogVisible"
      width="90%"
      ref="editDialog"
      @close="editFormClose"
      :close-on-click-modal="false"
      v-dialogDrag
    >
      <el-steps :active="activeName" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="前期资料"></el-step>
        <el-step title="政府或上级部门批复资料"></el-step>
        <el-step title="资金来源情况表"></el-step>
        <el-step title="政府采购计划备案表、采购方式说明"></el-step>
        <el-step title="采购需求、评分标准"></el-step>
        <el-step title="委托代理协议书、确定代理机构党组会议纪要"></el-step>
        <el-step title="中标通知书"></el-step>
        <el-step title="政府采购合同"></el-step>
        <el-step title="合同履约验收书，包含验收申请、签收资料等"></el-step>
        <el-step
          title="资金支付资料——资金申请文件、领导批示签字等资料"
        ></el-step>
        <el-step title="其他资料"></el-step>
      </el-steps>

      <el-form
        ref="editBaseInfoForm"
        :model="form"
        :rules="rules"
        label-width="120px"
        v-if="activeName === 0"
      >
        <el-form-item label="项目名称" prop="demandName">
          <el-input
            v-model="form.demandName"
            placeholder="请输入项目名称"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
      </el-form>

      <el-form
        ref="editFormUpload1"
        :model="editFormUpload1"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 1"
      >
        <el-form-item label="文件上传" prop="fileRules">
          <audit-upload
            :upload-address="upload.uploadAddress1"
            :type="1"
            :auth-disabled="isClickLookBtn"
            :project-id="uploadProjectId"
            :isopen="editDialogVisible"
            ref="upload1"
          ></audit-upload>
        </el-form-item>
      </el-form>

      <el-form
        ref="editFormUpload2"
        :model="editFormUpload2"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 2"
      >
        <el-form-item label="文件上传" prop="fileRules">
          <audit-upload
            :upload-address="upload.uploadAddress2"
            :type="2"
            ref="upload2"
            :auth-disabled="isClickLookBtn"
            :project-id="uploadProjectId"
          ></audit-upload>
        </el-form-item>
      </el-form>

      <el-form
        ref="editBaseInfoForm3"
        :model="editForm3"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 3"
      >
        <el-form-item label="投资总额" prop="totalinvestment">
          <el-input
            v-model="editForm3.totalinvestment"
            placeholder="请输入投资总额/万元"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
        <el-form-item label="预算金额" prop="budget">
          <el-input
            v-model="editForm3.budget"
            placeholder="请输入预算金额/万元"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
        <el-form
          ref="editFormUpload3"
          :model="editFormUpload3"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress3"
              :type="3"
              ref="upload3"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editBaseInfoForm4"
        :model="editForm4"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 4"
      >
        <el-form-item label="备案时间" prop="beianTime">
          <el-date-picker
            v-model="editForm4.beianTime"
            type="date"
            placeholder="请选择备案时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form
          ref="editFormUpload4"
          :model="editFormUpload4"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress4"
              :type="4"
              ref="upload4"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editFormUpload5"
        :model="editFormUpload5"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 5"
      >
        <el-form-item label="文件上传" prop="fileRules">
          <audit-upload
            :upload-address="upload.uploadAddress5"
            :type="5"
            ref="upload5"
            :auth-disabled="isClickLookBtn"
            :project-id="uploadProjectId"
          ></audit-upload>
        </el-form-item>
      </el-form>

      <el-form
        ref="editBaseInfoForm6"
        :model="editForm6"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 6"
      >
        <el-form-item label="公示时间" prop="gongshiTime">
          <el-date-picker
            v-model="editForm6.gongshiTime"
            type="date"
            placeholder="请选择公示时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开标时间" prop="kaibiaoTime">
          <el-date-picker
            v-model="editForm6.kaibiaoTime"
            type="date"
            placeholder="请选择开标时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form
          ref="editFormUpload6"
          :model="editFormUpload6"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress6"
              :type="6"
              ref="upload6"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editBaseInfoForm7"
        :model="editForm7"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 7"
      >
        <el-form-item label="中标时间" prop="zhongbiaoTime">
          <el-date-picker
            v-model="editForm7.zhongbiaoTime"
            type="date"
            placeholder="请选择中标时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="中标金额" prop="bidMoney">
          <el-input
            v-model="editForm7.bidMoney"
            placeholder="请输入中标金额/万元"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
        <el-form-item label="中标供应商" prop="bidSupplier">
          <el-input
            v-model="editForm7.bidSupplier"
            placeholder="请输入中标标供应商"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
        <el-form
          ref="editFormUpload7"
          :model="editFormUpload7"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress7"
              :type="7"
              ref="upload7"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editBaseInfoForm8"
        :model="editForm8"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 8"
      >
        <el-form-item label="合同时间" prop="contractTime">
          <el-date-picker
            v-model="editForm8.contractTime"
            type="date"
            placeholder="请选择合同时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form
          ref="editFormUpload8"
          :model="editFormUpload8"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress8"
              :type="8"
              ref="upload8"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editBaseInfoForm9"
        :model="editForm9"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 9"
      >
        <el-form-item label="验收时间" prop="yanshouTime">
          <el-date-picker
            v-model="editForm9.yanshouTime"
            type="date"
            placeholder="请选择验收时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form
          ref="editFormUpload9"
          :model="editFormUpload9"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress9"
              :type="9"
              ref="upload9"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editBaseInfoForm10"
        :model="editForm10"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 10"
      >
        <el-form-item label="第一次支付金额" prop="firstMoney">
          <el-input
            v-model="editForm10.firstMoney"
            placeholder="请输入第一次支付金额/万元"
            :disabled="isClickLookBtn"
          />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker
            v-model="editForm10.payTime"
            type="date"
            placeholder="请选择支付时间"
            :disabled="isClickLookBtn"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form
          ref="editFormUpload10"
          :model="editFormUpload10"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="文件上传" prop="fileRules">
            <audit-upload
              :upload-address="upload.uploadAddress10"
              :type="10"
              ref="upload10"
              :auth-disabled="isClickLookBtn"
              :project-id="uploadProjectId"
            ></audit-upload>
          </el-form-item>
        </el-form>
      </el-form>

      <el-form
        ref="editFormUpload11"
        :model="editFormUpload11"
        :rules="rules"
        label-width="120px"
        v-show="activeName === 11"
      >
        <el-form-item label="文件上传">
          <audit-upload
            :upload-address="upload.uploadAddress11"
            :type="11"
            ref="upload11"
            :auth-disabled="isClickLookBtn"
            :project-id="uploadProjectId"
          ></audit-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          :disabled="activeName === 0"
          type="primary"
          size="small"
          icon="el-icon-caret-left"
          @click="changeStep(-1)"
          >上一步</el-button
        >
        <el-button
          type="primary"
          size="small"
          icon="el-icon-folder-checked"
          @click="submitEditForm()"
          :disabled="isClickLookBtn"
          >保 存</el-button
        >
        <el-button
          :disabled="activeName === 11"
          type="primary"
          size="small"
          icon="el-icon-caret-right"
          @click="changeStep(+1)"
          >下一步</el-button
        >
        <!-- <el-button type="primary" @click="submitEditForm">确 定</el-button> -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDemand,
  getDemand,
  delDemand,
  addDemand,
  updateDemand,
  getStage,
  queryList,
  confirmProject,
  recallProject,
} from "@/api/archives/purchase";
import { getToken } from "@/utils/auth";
import AuditUpload from "@/views/archives/components/AuditUpload";

export default {
  name: "Demand",
  components: {
    AuditUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购类项目需求申请表格数据
      demandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        demandName: null,
        userId: null,
        deptId: null,
        schoolName: null,
        totalinvestment: null,
        budget: null,
        beianTime: null,
        gongshiTime: null,
        kaibiaoTime: null,
        zhongbiaoTime: null,
        bidMoney: null,
        bidSupplier: null,
        contractTime: null,
        yanshouTime: null,
        firstMoney: null,
        payTime: null,
        confirm: null,
        jd: null,
        selectStartTime: null,
        selectEndTime: null,
        jdId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        demandName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        totalinvestment: [
          { required: true, message: "投资总额不能为空", trigger: "blur" },
        ],
        budget: [
          { required: true, message: "预算资金不能为空", trigger: "blur" },
        ],
        beianTime: [
          { required: true, message: "备案时间不能为空", trigger: "blur" },
        ],
        gongshiTime: [
          { required: true, message: "公示时间不能为空", trigger: "blur" },
        ],
        kaibiaoTime: [
          { required: true, message: "开标时间不能为空", trigger: "blur" },
        ],
        zhongbiaoTime: [
          { required: true, message: "中标时间不能为空", trigger: "blur" },
        ],
        bidMoney: [
          { required: true, message: "中标金额不能为空", trigger: "blur" },
        ],
        bidSupplier: [
          { required: true, message: "中标供应商不能为空", trigger: "blur" },
        ],
        contractTime: [
          { required: true, message: "合同时间不能为空", trigger: "blur" },
        ],
        yanshouTime: [
          { required: true, message: "验收时间不能为空", trigger: "blur" },
        ],
        firstMoney: [
          {
            required: true,
            message: "第一次支付金额不能为空",
            trigger: "blur",
          },
        ],
        payTime: [
          { required: true, message: "支付时间不能为空", trigger: "blur" },
        ],
        fileRules: [{ required: true, message: "请上传文件", trigger: "blur" }],
      },
      editDialogVisible: false,
      editForm: {},
      editForm1: {},
      editFormUpload1: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm2: {},
      editFormUpload2: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm3: {},
      editFormUpload3: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm4: {},
      editFormUpload4: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm5: {},
      editFormUpload5: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm6: {},
      editFormUpload6: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm7: {},
      editFormUpload7: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm8: {},
      editFormUpload8: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm9: {},
      editFormUpload9: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm10: {},
      editFormUpload10: {
        uploadAddress: "",
        fileRules: null,
      },
      editForm11: {},
      editFormUpload11: {
        uploadAddress: "",
        fileRules: null,
      },
      activeName: 0,
      uploadProjectId: 0,
      confirmStateOptions: [
        {
          value: 0,
          label: "未确认",
        },
        {
          value: 1,
          label: "已确认",
        },
      ],
      jdOptions: [
        {
          value: "项目备案",
          label: "项目备案",
        },
        {
          value: "项目公示",
          label: "项目公示",
        },
        {
          value: "项目中标",
          label: "项目中标",
        },
        {
          value: "项目施工",
          label: "项目施工",
        },
        {
          value: "项目验收",
          label: "项目验收",
        },
      ],
      editFormTitle: "",
      isClickLookBtn: false,
      upload: {
        uploadAddress1: "",
        uploadAddress2: "",
        uploadAddress3: "",
        uploadAddress4: "",
        uploadAddress5: "",
        uploadAddress6: "",
        uploadAddress7: "",
        uploadAddress8: "",
        uploadAddress9: "",
        uploadAddress10: "",
        uploadAddress11: "",
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$bus.$on("fileUploadSuccess", (fileName, type) => {
      // console.log(fileName, type);
      switch (type) {
        case 1:
          this.editFormUpload1.fileRules = fileName;
          break;
        case 2:
          this.editFormUpload2.fileRules = fileName;
          break;
        case 3:
          this.editFormUpload3.fileRules = fileName;
          break;
        case 4:
          this.editFormUpload4.fileRules = fileName;
          break;
        case 5:
          this.editFormUpload5.fileRules = fileName;
          break;
        case 6:
          this.editFormUpload6.fileRules = fileName;
          break;
        case 7:
          this.editFormUpload7.fileRules = fileName;
          break;
        case 8:
          this.editFormUpload8.fileRules = fileName;
          break;
        case 9:
          this.editFormUpload9.fileRules = fileName;
          break;
        case 10:
          this.editFormUpload10.fileRules = fileName;
          break;
      }
      // this.editFormUpload1.fileRules1 = fileName;
    });
  },
  methods: {
    /** 查询采购类项目需求申请列表 */
    getList() {
      this.loading = true;
      listDemand(this.queryParams).then((response) => {
        // console.log(response);
        this.demandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        demandName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // queryList(this.queryParams);
      // console.log(this.queryParams);
      this.queryParams.selectEndTime =
        this.queryParams.selectStartTime && this.queryParams.selectStartTime[1];
      this.queryParams.selectStartTime =
        this.queryParams.selectStartTime && this.queryParams.selectStartTime[0];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.confirm = null;
      this.queryParams.jd = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.buyDemandId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增项目";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        // console.log(this.form);
        addDemand(this.form).then((res) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
        /* if (valid) {
          if (this.form.buyDemandId != null) {
            updateDemand(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDemand(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        } */
      });
    },
    // 修改 / 查看
    handleUpdate(row, type) {
      console.log(row);
      this.activeName=row.jdId ? row.jdId : 0
      if (type === 0) {
        this.editFormTitle = "查看申请";
        this.isClickLookBtn = true;
      } else {
        this.editFormTitle = "修改申请";
      }
      this.editDialogVisible = true;

      setTimeout(() => {
        this.$refs.upload1.queryFile(row.buyDemandId, 1);
        this.$refs.upload2.queryFile(row.buyDemandId, 2);
        this.$refs.upload3.queryFile(row.buyDemandId, 3);
        this.$refs.upload4.queryFile(row.buyDemandId, 4);
        this.$refs.upload5.queryFile(row.buyDemandId, 5);
        this.$refs.upload6.queryFile(row.buyDemandId, 6);
        this.$refs.upload7.queryFile(row.buyDemandId, 7);
        this.$refs.upload8.queryFile(row.buyDemandId, 8);
        this.$refs.upload9.queryFile(row.buyDemandId, 9);
        this.$refs.upload10.queryFile(row.buyDemandId, 10);
        this.$refs.upload11.queryFile(row.buyDemandId, 11);
      }, 500);

      getDemand(row.buyDemandId).then((res) => {
        // console.log(res);
        this.uploadProjectId = res.data.buyDemandId;
        this.upload.uploadAddress1 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/1`;
        this.upload.uploadAddress2 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/2`;
        this.upload.uploadAddress3 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/3`;
        this.upload.uploadAddress4 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/4`;
        this.upload.uploadAddress5 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/5`;
        this.upload.uploadAddress6 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/6`;
        this.upload.uploadAddress7 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/7`;
        this.upload.uploadAddress8 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/8`;
        this.upload.uploadAddress9 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/9`;
        this.upload.uploadAddress10 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/10`;
        this.upload.uploadAddress11 = `${process.env.VUE_APP_BASE_URL}/archives/buyFiles/upload/${res.data.buyDemandId}/11`;
        this.form =
          this.editForm1 =
          this.editForm2 =
          this.editForm3 =
          this.editForm4 =
          this.editForm5 =
          this.editForm6 =
          this.editForm7 =
          this.editForm8 =
          this.editForm9 =
          this.editForm10 =
          this.editForm11 =
            res.data;
      });
    },
    // 提交修改表单
    submitEditForm() {
      switch (this.activeName) {
        case 0:
          this.$refs.editBaseInfoForm.validate((valid) => {
            if (!valid) return;
            // console.log(this.form);
            updateDemand({ ...this.form, jdId: 0 }).then((res) => {
              this.$modal.msgSuccess("保存成功");
            });
          });
          break;
        case 1:
          this.$refs.editFormUpload1.validate((valid) => {
            if (!valid) return;
            updateDemand({ ...this.editForm1, jdId: 1 }).then((res) => {
              this.$modal.msgSuccess("保存成功");
            });
          });
          break;
        case 2:
          this.$refs.editFormUpload2.validate((valid) => {
            if (!valid) return;
            updateDemand({ ...this.editForm2, jdId: 2 }).then((res) => {
              this.$modal.msgSuccess("保存成功");
            });
          });
          break;
        case 3:
          this.$refs.editBaseInfoForm3.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload3.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm3, jdId: 3 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 4:
          this.$refs.editBaseInfoForm4.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload4.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm4, jdId: 4 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 5:
          this.$refs.editFormUpload5.validate((valid) => {
            if (!valid) return;
            updateDemand({ ...this.editForm5, jdId: 5 }).then((res) => {
              this.$modal.msgSuccess("保存成功");
            });
          });
          break;
        case 6:
          this.$refs.editBaseInfoForm6.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload6.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm6, jdId: 6 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 7:
          this.$refs.editBaseInfoForm7.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload7.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm7, jdId: 7 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 8:
          this.$refs.editBaseInfoForm8.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload8.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm8, jdId: 8 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 9:
          this.$refs.editBaseInfoForm9.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload9.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm9, jdId: 9 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 10:
          this.$refs.editBaseInfoForm10.validate((valid) => {
            if (!valid) return;
            this.$refs.editFormUpload10.validate((valid) => {
              if (!valid) return;
              updateDemand({ ...this.editForm10, jdId: 10 }).then((res) => {
                this.$modal.msgSuccess("保存成功");
              });
            });
          });
          break;
        case 11:
          updateDemand({ ...this.editForm11, jdId: 11 }).then((res) => {
            this.$modal.msgSuccess("保存成功");
          });
          break;
      }
    },
    // 监听修改对话框的关闭
    editFormClose() {
      this.editDialogVisible = false;
      !this.isClickLookBtn && this.getList();
      this.isClickLookBtn = false;
      this.form =
        this.editForm1 =
        this.editForm2 =
        this.editForm3 =
        this.editForm4 =
        this.editForm5 =
        this.editForm6 =
        this.editForm7 =
        this.editForm8 =
        this.editForm9 =
        this.editForm10 =
        this.editForm11 =
          {};
      this.editFormUpload1 =
        this.editFormUpload2 =
        this.editFormUpload3 =
        this.editFormUpload4 =
        this.editFormUpload5 =
        this.editFormUpload6 =
        this.editFormUpload7 =
        this.editFormUpload8 =
        this.editFormUpload9 =
        this.editFormUpload10 =
        this.editFormUpload11 =
          {
            uploadAddress: "",
            fileRules: null,
          };
      this.upload = {
        uploadAddress1: "",
        uploadAddress2: "",
        uploadAddress3: "",
        uploadAddress4: "",
        uploadAddress5: "",
        uploadAddress6: "",
        uploadAddress7: "",
        uploadAddress8: "",
        uploadAddress9: "",
        uploadAddress10: "",
        uploadAddress11: "",
      };
      this.resetForm("editBaseInfoForm");
      this.resetForm("editBaseInfoForm1");
      this.resetForm("editBaseInfoForm2");
      this.resetForm("editBaseInfoForm3");
      this.resetForm("editBaseInfoForm4");
      this.resetForm("editBaseInfoForm5");
      this.resetForm("editBaseInfoForm6");
      this.resetForm("editBaseInfoForm7");
      this.resetForm("editBaseInfoForm8");
      this.resetForm("editBaseInfoForm9");
      this.resetForm("editBaseInfoForm10");
      this.resetForm("editBaseInfoForm11");

      this.resetForm("editFormUpload1");
      this.resetForm("editFormUpload2");
      this.resetForm("editFormUpload3");
      this.resetForm("editFormUpload4");
      this.resetForm("editFormUpload5");
      this.resetForm("editFormUpload6");
      this.resetForm("editFormUpload7");
      this.resetForm("editFormUpload8");
      this.resetForm("editFormUpload9");
      this.resetForm("editFormUpload10");
      this.resetForm("editFormUpload11");

      this.uploadProjectId = 0;
      this.activeName = 0;
    },
    changeStep(num) {
      if (num < 0) {
        this.activeName = this.activeName + num;
      }
      if (num > 0 && this.isClickLookBtn) {
        this.activeName = this.activeName + num;
      }
      if (num > 0 && !this.isClickLookBtn) {
        switch (this.activeName) {
          case 0:
            if (!this.form.demandName) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 1:
            /* if (!this.editFormUpload1.fileRules) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            } */
            this.activeName = this.activeName + num;
            break;
          case 2:
            /* if (!this.editFormUpload2.fileRules) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            } */
            this.activeName = this.activeName + num;
            break;
          case 3:
            if (!this.editForm3.totalinvestment || !this.editForm3.budget) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 4:
            if (!this.editForm4.beianTime) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 5:
            /*  if (!this.editFormUpload5.fileRules) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            } */
            this.activeName = this.activeName + num;
            break;
          case 6:
            if (!this.editForm6.gongshiTime || !this.editForm6.kaibiaoTime) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 7:
            if (
              !this.editForm7.zhongbiaoTime ||
              !this.editForm7.bidMoney ||
              !this.editForm7.bidSupplier
            ) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 8:
            if (!this.editForm8.contractTime) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 9:
            if (!this.editForm9.yanshouTime) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 10:
            if (!this.editForm10.firstMoney || !this.editForm10.payTime) {
              this.$message.error("您有未录入的信息");
            } else {
              this.activeName = this.activeName + num;
            }
            break;
          case 11:
            break;
          default:
            break;
        }
      }
    },

    // 确认项目
    confirmBtnClick(row) {
      this.$confirm("确认之后用户将不可修改, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          confirmProject(row.buyDemandId).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已撤销!",
          });
        });
    },
    // 撤回
    recallBtnClick(row) {
      this.$confirm("撤回后该项目可以修改, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          recallProject(row.buyDemandId).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已撤销!",
          });
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // console.log(row);
      const buyDemandIds = row.buyDemandId || this.ids;
      this.$confirm("你确定要删除这一项吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDemand(row.buyDemandId).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleDownload(row) {
      this.$download.zip(
        `/archives/buyFiles/buyDemand/downloadAll/${row.buyDemandId}`,
        `caigou_${row.schoolName}_${row.demandName}_${new Date().getTime()}`
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/archives/buyDemand/bexport",
        {
          ...this.queryParams,
        },
        `caigou_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
